package com.jince.datacenter.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jince.datacenter.enums.BusinType;
import com.jince.datacenter.mapper.DayActivityMapper;
import com.jince.datacenter.model.DayActivity;
import com.jince.datacenter.utils.DateUtil;

/**
 * 
 * @author ban.wu
 *
 */
@Service
public class DataImportService {

	private final static Logger logger = Logger
			.getLogger(DataImportService.class);

	@Autowired
	private DayActivityMapper dayActivityMapper;

	public void importData(String path) {

		File pathFile = new File(path);
		if (!pathFile.exists()) {
			logger.info("本地目录不存在! " + path);
			return;
		}

		FileFilter filefilter = new FileFilter() {
			@Override
			public boolean accept(File file) {
				// if the file extension is .txt return true, else false
				if (file.getName().endsWith(".txt")) {
					return true;
				}
				return false;
			}
		};

		File[] files = pathFile.listFiles(filefilter);

		for (File file : files) {

			File okLocalFile = new File(path + File.separator + file.getName()
					+ ".ok");
			if (okLocalFile.exists()) {
				logger.debug("文件：" + file.getName() + "已处理！");
				continue;
			}

			String fileName = file.getName();
			String businType = fileName.split("_")[2];
			switch (BusinType.valueOf(businType)) {
			case activeuser:
				importActiveUserData(path, file);
				break;

			default:
				break;
			}
		}
	}

	public void importActiveUserData(String path, File file) {
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new FileReader(file));
			String lineTxt = null;
			while ((lineTxt = bufferedReader.readLine()) != null) {
				String[] strArr = lineTxt.trim().split("\\|");
				DayActivity dayActivity = new DayActivity();
				dayActivity.setNatureDate(DateUtil.getStrToDate(
						DateUtil.yyyyMMdd, strArr[0]));
				dayActivity.setNewCounts(Integer.valueOf(strArr[1]));
				dayActivity.setOldCounts(Integer.valueOf(strArr[2]));
				dayActivity.setCreateTime(new Date());
				dayActivityMapper.insert(dayActivity);
			}

			File okLocalFile = new File(path + File.separator + file.getName()
					+ ".ok");
			if (!okLocalFile.exists()) {
				try {
					okLocalFile.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
					logger.error(e.getMessage(), e);
				}
			}
			logger.info("文件：" + file.getName() + "处理完成！");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		} catch (IOException e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
					logger.error(e.getMessage(), e);
				}
			}
		}
	}
}
